﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using Project.Models;

namespace Project.Controllers
{
    public class SpacesController : Controller
    {
        private projektlaborEntities db = new projektlaborEntities();
        // GET: Spaces
        public async Task<ActionResult> Index()
        {
            var spaces = db.Spaces.Include(s => s.Columns).Include(s => s.Levels).Include(s => s.Rows);
            return View(await spaces.ToListAsync());
        }
        public async Task<ActionResult> FreeIndex(int? warehouseitemid)
        {
            ViewBag.WarehouseItemId = warehouseitemid;
            var spaces = db.Spaces.Where(s => s.IsFull != true).Include(s => s.Columns).Include(s => s.Levels).Include(s => s.Rows);
            return View(await spaces.ToListAsync());
        }

        // GET: Spaces/Details/5
        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Spaces spaces = await db.Spaces.FindAsync(id);
            if (spaces == null)
            {
                return HttpNotFound();
            }
            return View(spaces);
        }

        // GET: Spaces/Create
        public ActionResult Create()
        {
            ViewBag.Column_Id = new SelectList(db.Columns, "Column_Id", "Column_Id");
            ViewBag.Level_Id = new SelectList(db.Levels, "Level_Id", "Level_Id");
            ViewBag.Row_Id = new SelectList(db.Rows, "Row_Id", "Row_Id");
            return View();
        }

        // POST: Spaces/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Create([Bind(Include = "Space_Id,Row_Id,Column_Id,Level_Id,IsFull")] Spaces spaces)
        {
            if (ModelState.IsValid)
            {
                db.Spaces.Add(spaces);
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }

            ViewBag.Column_Id = new SelectList(db.Columns, "Column_Id", "ColumnName", spaces.Column_Id);
            ViewBag.Level_Id = new SelectList(db.Levels, "Level_Id", "LevelName", spaces.Level_Id);
            ViewBag.Row_Id = new SelectList(db.Rows, "Row_Id", "RowName", spaces.Row_Id);
            
            return View(spaces);
        }


        public async Task<ActionResult> Add(int id, int warehouseitemid)
        {
            WarehouseItems warehouseItems = await db.WarehouseItems.FindAsync(warehouseitemid);
            warehouseItems.Space_Id = id;
            db.Entry(warehouseItems).State = EntityState.Modified;
            await db.SaveChangesAsync();
            Spaces spaces = await db.Spaces.FindAsync(warehouseItems.Space_Id);
            spaces.IsFull = true;
            db.Entry(spaces).State = EntityState.Modified;
            await db.SaveChangesAsync();
            return RedirectToAction("Index", "WarehouseItems");
        }

        // GET: Spaces/Edit/5
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Spaces spaces = await db.Spaces.FindAsync(id);
            if (spaces == null)
            {
                return HttpNotFound();
            }
            ViewBag.Column_Id = new SelectList(db.Columns, "Column_Id", "Column_Id", spaces.Column_Id);
            ViewBag.Level_Id = new SelectList(db.Levels, "Level_Id", "Level_Id", spaces.Level_Id);
            ViewBag.Row_Id = new SelectList(db.Rows, "Row_Id", "Row_Id", spaces.Row_Id);
            
            return View(spaces);
        }

        // POST: Spaces/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Edit([Bind(Include = "Space_Id,Row_Id,Column_Id,Level_Id,IsFull")] Spaces spaces)
        {
            if (ModelState.IsValid)
            {
                db.Entry(spaces).State = EntityState.Modified;
                await db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.Column_Id = new SelectList(db.Columns, "Column_Id", "Column_Id", spaces.Column_Id);
            ViewBag.Level_Id = new SelectList(db.Levels, "Level_Id", "Level_Id", spaces.Level_Id);
            ViewBag.Row_Id = new SelectList(db.Rows, "Row_Id", "Row_Id", spaces.Row_Id);
            
            return View(spaces);
        }

        // GET: Spaces/Delete/5
        public async Task<ActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Spaces spaces = await db.Spaces.FindAsync(id);
            if (spaces == null)
            {
                return HttpNotFound();
            }
            return View(spaces);
        }

        // POST: Spaces/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> DeleteConfirmed(int id)
        {
            Spaces spaces = await db.Spaces.FindAsync(id);
            db.Spaces.Remove(spaces);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
